//
//  CustomAnnotationView.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/10/15.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "AnnotationViewForMallHome.h"

@interface AnnotationViewForMallHome ()

@property (nonatomic, strong, readwrite) CustomCalloutView *calloutView;

@end

#define kCalloutWidth       200.0
#define kCalloutHeight      70.0

@implementation AnnotationViewForMallHome

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
	if (self.selected == selected)
	{
		return;
	}
	
	if (selected)
	{
		if (self.calloutView == nil)
		{
			self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
			self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
												  -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
		}
		int i = 0;
		for(ShopLocationInfo *model in self.modelArr){
			if (model.lat == self.annotation.coordinate.latitude && model.lng == self.annotation.coordinate.longitude) {
				self.calloutView.imageStr = model.photo;
				[[NSNotificationCenter defaultCenter] postNotificationName:@"BottomTableScrollToIndex" object:nil userInfo:@{@"index":[NSString stringWithFormat:@"%d",i]}];
			}
			i++;
		}
		self.calloutView.title = self.annotation.title;
		self.calloutView.subtitle = self.annotation.subtitle;
		
		//做了自适应。重新定位中心点
		self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
											  -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
		
		[self addSubview:self.calloutView];
		NSLog(@"i was here to add view");
	}
	else
	{
		[self.calloutView removeFromSuperview];
		NSLog(@"i was here to remove view");
	}
	
	[super setSelected:selected animated:animated];
}

- (NSArray *)modelArr
{
	if (!_modelArr) {
		_modelArr = [[NSArray alloc] init];
	}
	return _modelArr;
}

@end
